package ADBook.Advance;
import java.awt.*;

import java.sql.*;
import java.util.Vector;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;



//the main UI to display the System
//two text four button
public class UI {
	/*
	 * 1. Define the combo
	 * 2. define the variable in the database
	 * 3. define the function of the button and click
	 * 4. display the info in the table
	 */
	
	//the main frame
	JFrame ADB_UI;
	//the text
	public JTextField name_input, phone_input;
	//the radiobutton
	public JRadioButton male_b, female_b;
	//the four buttons
	public JButton add_b, delete_b, update_b, query_b;
	//the label
	JLabel name_l, phone_l;
	//the buttongroup to combine the radiobutton
	ButtonGroup buttongroup;
	JPanel panel;
	//the table to show the info
	JTable infotable;
	DefaultTableModel infotable_model;
	JScrollPane ScrollPane;
	public Vector vdata = new Vector();
	public Vector vcoldata = new Vector();
	
	//to connect to the database
	String USERNAME = "root";
	String PASSWORD = "zhc20040309";
	String URL = "jdbc:mysql://localhost:3306/hw?serverTimezone=UTC";
	Connection conn;
	Statement stmt;
	ResultSet rest;
	
	//construct function
	public UI(String title){
		//installation of all the combo
		ADB_UI = new JFrame(title);
		ADB_UI.setSize(800, 500);
		//label
		name_l = new JLabel("姓名");
		phone_l = new JLabel("电话号码");
		//installation of the button
		add_b = new JButton("添加");
		delete_b = new JButton("删除");
		update_b = new JButton("修改");
		query_b = new JButton("查询");
		//radiobutton
		male_b = new JRadioButton("男");
		female_b = new JRadioButton("女");
		buttongroup = new ButtonGroup();
		buttongroup.add(male_b);
		buttongroup.add(female_b);
		panel = new JPanel();
		panel.add(male_b);
		panel.add(female_b);
		//textfield
		name_input = new JTextField(15);
		phone_input = new JTextField(15);
		//create the connection
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
			PreparedStatement pstmt = conn.prepareStatement("select * from ADB");
			rest = pstmt.executeQuery();
			while(rest.next()) {
				Vector row = new Vector();
				row.add(rest.getString(1));
				row.add(rest.getString(2));
				row.add(rest.getString(3));
				vdata.add(row);
			}
			//重点!!!这里一定要记得添加内容!
			vcoldata.add("姓名");
			vcoldata.add("性别");
			vcoldata.add("电话号码");
		} catch (ClassNotFoundException e) {
			System.out.print("无法创建对象");
		} catch (SQLException e) {
			System.out.print("无法连接到数据库");
		}
		//JTable
		infotable_model = new DefaultTableModel(vdata, vcoldata);
		infotable = new JTable(infotable_model);
		ScrollPane = new JScrollPane(infotable);
		
		//display 
		ADB_UI.add(name_l);
		ADB_UI.add(name_input);
		//这里注意添加了panel就不要添加两个按钮了
		ADB_UI.add(panel);
		ADB_UI.add(phone_l);
		ADB_UI.add(phone_input);
		ADB_UI.add(ScrollPane);
		ADB_UI.add(add_b);
		ADB_UI.add(delete_b);
		ADB_UI.add(update_b);
		ADB_UI.add(query_b);
		
		ADB_UI.setLayout(new FlowLayout());
		ADB_UI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		ADB_UI.setVisible(true);
		
	}
	
	
}
